// pages/group/group.js
const group = require("../../utils/group");
const { groups } = require("../../utils/group");
var g = require("../../utils/group")
Page({
    matchname:function (e) {
      this.setData({
        matchname:e.detail.value
      });
    },
    cansai(){
      var matchid = this.data.result[this.data.index]._id;
      var title = this.data.result[this.data.index].title;
      var userid = this.data.userid;
      var username = this.data.username;
      var database= wx.cloud.database();
      database.collection("baoming").add({
        data:{
          matchid:matchid,
          title:title,
          userid:userid,
          username:username
        }
      }).then((res)=>{
        if (res){
          wx.showToast({
          title: '报名成功',})
        }else{
          wx.showToast({
            title: '报名失败',
          })
        }
      }).catch((err)=>{
        console.log(err);
      });
    },
    create(){
      var that=this
      var database=wx.cloud.database();
      database.collection("match").add({
        data:{
          title:that.data.matchname,
          userid:that.data.userid,
          imageList:that.data.imageList
        }
      }).then((res)=>{
        console.log("插入一条数据后数据库返回的数据:"+res);
        if(res){
          wx.showToast({
            title: '数据插入成功',
          })
        }else{
          wx.showToast({
            title: '数据插入失败',
          })
        }
      })
    },

    picname:function (e) {
      this.setData({
        picname:e.detail.value
      });
    },
    upload(){
        var that = this
        wx.chooseMedia({
            count: 1,
            success(res) {
                console.log(res.tempFiles)
                var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
                var ext = res.tempFiles[0].tempFilePath.slice(po)
                wx.cloud.uploadFile({
                    cloudPath: that.data.picname + ext,
                    filePath: res.tempFiles[0].tempFilePath,
                    success(res) {
                        console.log(res.fileID)
                        that.data.imageList.push(res.fileID);
                        that.setData({
                          imageList:that.data.imageList
                        });
                    }
                })
            }
        })
    },
    choose:function (e) {
     var k = e.currentTarget.id;
     var loc = this.data.imageList[k];
      this.setData({
        currentImage:loc,
        flag3:true,
        deleteindex:k
      })
    },
    delete(){
      var k=this.data.deleteindex;
      this.data.imageList.splice(k,1);
      this.setData({
        imageList:this.data.imageList,
        flag3:false
      });
    },
    /**
     * 页面的初始数据
     */
    data: {
        username:null,
        xuanshou:[],
        deleteindex:0,
        flag3:false,
        currentImage:null,
        imageList:[],
        picname:null,
        userid:null,
        matchname:null,
        titles:g.list,
        category:0,
        neirong:null,
        result:[],
        index:0,
        flag:false,
        flag2:false,
        matches:[],

    },
    changeCategory:function(e){
        this.setData({
          category:e.currentTarget.id
        });
    },shuru:function(e){
        this.setData({
          neirong:e.detail.value
        });
    },click(){
        var neirong = this.data.neirong;
        var gourps = g.groups;
        var result = [];
        for(let i = 0;i < groups.length;i++){
          var data = groups[i];
          if(data.name.match(neirong)){
            result.push(data);
          }
        }
        this.setData({
            result:result
        });
    },tapback:function(e){
        this.setData({
          index:e.currentTarget.id,
          flag:true
        });
    },dianji:function(e){
        var neirong = this.data.neirong;
        var database = wx.cloud.database();
        database.collection("match").where({}).get().then((res)=>{
            this.data.matches = res.data;
            var matches = this.data.matches;
            var result = [];
    
            for(let i = 0;i < matches.length;i++){
              var data = matches[i];
              if(data.title.match(neirong)){
                result.push(data);
              }
            }
            this.setData({
              result:result
            });
        }).catch((err)=>{
            console.log(err);
        });
    },tapback2:function(e){
      var database=wx.cloud.database();
      var k=e.currentTarget.id;
      var data=this.data.result[k];
      var that=this;
      database.collection("baoming").where({
        matchid:data._id
      }).get().then((res)=>{
        that.setData({
          xuanshou:res.data
        });
      });
      this.setData({
        index:e.currentTarget.id,
        flag2:true
      });
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that=this;
      wx.getStorage({
        key:"userid",
        success(res){
          that.setData({
          userid:res.data
          });
          var database = wx.cloud.database();
          database.collection("user").where({
            _id:res.data
          }).get().then((msg)=>{
            console.log("返回长度"+msg.data.length);
            that.setData({
              username:msg.data[0].name
            });
          }).catch((err)=>{
            console.log(err);
          })
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})